<template>
  <div class="app-container calendar-list-container">
    <div class="filter-container">
      <el-button class="filter-item" @click="handleAdd()" type="primary" icon="el-icon-plus">添加</el-button>
      <el-input class="filter-item" @keyup.enter.native="handleFilter" style="width: 200px;"  placeholder="请输入关键词" v-model="search.keyword"></el-input>
      <el-button class="filter-item" v-waves icon="el-icon-search" @click="handleFilter">搜索</el-button>
    </div>
    <el-table
       v-loading="!pload"
       @selection-change = "selectionChange"
        ref="multipleTable"
        border
        :data="tableData"
       >
        <el-table-column
          type="selection"
          width="55"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="companyId"
          label="Id"
          align="center"
          show-overflow-tooltip
          width="100"
          >
        </el-table-column>
        <el-table-column
          property="companyName"
          label="名称"
          show-overflow-tooltip
          >
        </el-table-column>
        <el-table-column
          label="上线时间"
          align="center"
          width="180"
          show-overflow-tooltip
          >
          <template slot-scope="scope">
            {{scope.row.onlineTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}')}}
          </template>
        </el-table-column>
        <el-table-column
          property="registeredCapital"
          label="注册资本"
          align="center"
          show-overflow-tooltip
          width="100"
          >
        </el-table-column>
        <el-table-column
          property="registeredAddress"
          label="注册地址"
          align="center"
          show-overflow-tooltip
          width="200"
          >
        </el-table-column>
        <el-table-column
          property="incomeRange"
          label="收益范围"
          align="center"
          show-overflow-tooltip
          width="100"
          >
        </el-table-column>
        <el-table-column
          property="sortNumber"
          label="排序序号"
          align="center"
          show-overflow-tooltip
          width="100"
          >
        </el-table-column>
	      <el-table-column
	        label="状态"
	        align="center"
	        width="100"
	        >
	        <template slot-scope="scope">
	          <el-tag v-if="!scope.row.status" type="success" size="mini">启用</el-tag>
	          <el-tag v-else type="danger" size="mini">停用</el-tag>
	        </template>
	      </el-table-column>
        <el-table-column
          label="操作"
          width="150"
          align="center"
        >
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
    </el-table>
    <div class="filter-bottom">
      <el-button-group class="filter-item">
          <el-button @click="toggleSelection(-1)">全选</el-button>
          <el-button @click="toggleSelection(0)" :disabled="!selection.length">反向选择</el-button>
          <el-button @click="toggleSelection(1)" :disabled="!selection.length">取消选择</el-button>
          <el-button @click="toggleDel" :disabled="!selection.length">删除选中数据</el-button>
      </el-button-group>
    </div>
    <div class="pagination-container">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
      :page-size="limit"
      :page-sizes="limits"
      background
      layout="sizes, prev, pager, next, jumper, total"
      :total="count">
      </el-pagination>
    </div>
    <edit v-if="edit" :id="selectTable.companyId" @getData="getData"></edit>
  </div>
</template>

<script>
    import { companyList, companyRemove } from '@/utils/api'
    import waves from '@/directive/waves' // 水波纹指令
    import edit from './edit'
    export default {
      directives: {
        waves
      },
      data() {
        return {
          tableData: [],
          search: {
            keyword: ''
          },
          selection: [],
          selectTable: {},
          pload: false,
          edit: false,
          currentPage: 1,
          limit: 20,
          limits: [10, 20, 50, 100],
          count: 0
        }
      },
      components: {
        edit
      },
      created() {
        this.getData()
      },
      methods: {

        selectionChange(selection) {
          this.selection = selection
        },
        toggleSelection(s) {
          if (s === -1) {
            this.tableData.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row, true)
            })
          }
          if (s === 0) {
            this.tableData.forEach(row => {
              this.$refs.multipleTable.toggleRowSelection(row)
            })
          }
          if (s === 1) {
            this.$refs.multipleTable.clearSelection()
          }
        },
        handleAdd() {
          this.selectTable = {}
          this.selectTable.method = 'add'
          this.edit = true
        },
        async handleDelete(index, row) {
          this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
            try {
              this.pload = false
              await companyRemove({ companyIds: row.companyId })
              this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
              this.getData()
            } catch (e) {
              this.pload = true
            }
          })
        },
        toggleDel() {
          this.$confirm('确认要删除吗', { type: 'warning' }).then(async() => {
            const ids = this.selection.map((i, index) => {
              return i.companyId
            }).join(',')
            try {
              this.pload = false
              await companyRemove({ companyIds: ids })
              this.$notify({ title: '删除成功', type: 'success', duration: 5000 })
              this.getData()
            } catch (e) {
              this.pload = true
            }
          })
        },
        handleEdit(index, row) {
          this.selectTable = JSON.parse(JSON.stringify(row))
          this.selectTable.method = 'edit'
          this.edit = true
        },
        async getData() {
          const data = this.JSONo(
            {
              pageNum: this.currentPage,
              pageSize: this.limit
            },
            this.search
          )
          this.pload = false
          const Datas = await companyList(data)
          this.tableData = Datas.data
          this.count = Datas.pageInfo.total
          this.pload = true
        },
        handleFilter() {
          this.currentPage = 0
          this.getData()
        },
        handleSizeChange(val) {
          console.log(`每页 ${val} 条`)
          this.limit = val
          this.getData()
        },
        handleCurrentChange(val) {
          this.currentPage = val
          this.getData()
        }
      }
    }
</script>
